package com.college.eduService.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.college.commonutil.result.Result;
import com.college.eduService.entity.EduTeacher;
import com.college.eduService.entity.query.TeacherQuery;
import com.college.eduService.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(description="讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@CrossOrigin//解决跨越
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    //查询讲师表所有数据
    @GetMapping("findAll")
    @ApiOperation(value="查询所有")
    private Result findAllTeacher(){
        //调用service的方法实现查询所有
        List<EduTeacher> list = teacherService.list(null);
        return Result.ok().data("items",list);
    }

    //逻辑删除删除
    @DeleteMapping("del/{id}")
    @ApiOperation(value="逻辑删除")
    private Result delTeacher(@PathVariable String id){
        boolean b = teacherService.removeById(id);
        if(b){
            return  Result.ok();
        }else {
            return  Result.error();
        }
    }

    //分页查询讲师表数据
    @PostMapping("findPage/{current}/{limit}")
    @ApiOperation(value="分页查询")
    private Result findPageTeacher(@PathVariable Integer current,@PathVariable Integer limit,
                                   @RequestBody(required = false) TeacherQuery teacherQuery){
        //分页工具类
        Page<EduTeacher> thpage=new Page<>(current,limit);
        //条件构造器
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper();
        //判断条件是否为空
        if((StringUtils.hasText(teacherQuery.getName()))){
            wrapper.like("name",teacherQuery.getName());
        }
        if(!(StringUtils.isEmpty(teacherQuery.getLevel()))){
            wrapper.eq("level",teacherQuery.getLevel());
        }
        if((StringUtils.hasText(teacherQuery.getBegin()))){
            wrapper.ge("gmt_create",teacherQuery.getBegin());
        }
        if((StringUtils.hasText(teacherQuery.getEnd()))){
            wrapper.le("gmt_modified",teacherQuery.getEnd());
        }
        wrapper.orderByDesc("gmt_create");
        //调用方法实现分页
        teacherService.page(thpage,wrapper);
        Map<String,Object> map = new HashMap<>();
        //分页数据
        map.put("records",thpage.getRecords());
        //总页数
        map.put("total",thpage.getTotal());
        return Result.ok().data("items",map);
    }

    //添加讲师
    @PostMapping("saveTeacher")
    @ApiOperation(value="添加讲师")
    private Result saveTeacher(@RequestBody EduTeacher teacher){
        boolean save = teacherService.save(teacher);
        if(save){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
    //根据id查询讲师
    @GetMapping("getTeacher/{id}")
    @ApiOperation(value="根据id查询讲师")
    private Result getTeacher(@PathVariable String id){
        EduTeacher byId = teacherService.getById(id);
        return Result.ok().data("teacher",byId);
    }
    //修改讲师
    @PostMapping("/UpdateTeacher")
    @ApiOperation(value="修改讲师")
    private Result UpdateTeacher(@RequestBody EduTeacher teacher){
        //先根据id查询 后修改
        boolean b = teacherService.updateById(teacher);
        if (b){
            return Result.ok();
        }
        else {
            return Result.error();
        }
    }
}
